import { Node, _decorator, Component, EventTouch, Size, size, v3, Vec3 } from "cc";
import { GameEventType } from "../Event/EnumDefine";
import { EventManager } from "../Event/EventManager";
import { SoundManager, SoundType } from "../SoundManager";
import { DataConfig } from "../Config/DataConfig";
import Common from "../Config/Common";
import SHtml from "../SHtml/SHtml";

const { ccclass, property } = _decorator;

/** 玩家移动管理 */
@ccclass
export default class PlayerMovement extends Component {

    @property(Node)
    playerNode: Node = null; // 玩家节点

    @property(Node)
    fingerNode: Node = null; // 引导手势节点

    private screenLeft: number = 0; // 屏幕左边界
    private screenRight: number = 0; // 屏幕右边界
    private playerPos: Vec3 = v3();
    private playerSize: Size = size(50, 100);

    protected onLoad(): void {
        SHtml.instance.pInit();
        SoundManager.instance;
        // 监听触摸事件
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
        EventManager.instance.registerEvent(GameEventType.Game_Over, this.onGameOver, this);

        this.onScreen();
        EventManager.instance.registerEvent(GameEventType.Screen_Change, this.onScreen, this);
    }

    protected start(): void {
        SHtml.instance.pLoading();
    }

    onScreen() {
        const screenSize = Common.instance.getScreenSize();
        this.screenLeft = -screenSize.width / 2 + this.playerSize.width; // 屏幕左边界
        this.screenRight = screenSize.width / 2 - this.playerSize.width; // 屏幕右边界
    }

    onGameOver() {
        // 移除监听触摸事件
        this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }

    onTouchStart(event: EventTouch) {
        // 隐藏引导手势
        if (this.fingerNode && this.fingerNode.active) {
            this.fingerNode.active = false;
            EventManager.instance.triggerEvent(GameEventType.Change_Roll_Speed, DataConfig.initSpeed);
            EventManager.instance.triggerEvent(GameEventType.Game_Start, null);
            SoundManager.instance.playSound(SoundType.Background, 0.5, true);
        }
        this.playerPos = this.playerNode.position.clone();

        SHtml.instance.pInteraction('touch-start');
    }

    onTouchMove(event: EventTouch) {
        // 更新方块的位置
        Common.instance.getScreenSize().width;
        const disX = event.getUILocation().x - event.getUIStartLocation().x;
        let posX = this.playerPos.x + disX;
        if (posX < this.screenLeft) {
            posX = this.screenLeft;
        }
        else if (posX > this.screenRight) {
            posX = this.screenRight;
        }
        this.playerNode.position = v3(posX, this.playerPos.y);
    }

    onTouchEnd(event: EventTouch) {
        // 触摸结束时重置起始位
        SHtml.instance.pInteraction('touch-end');
    }
}